package com.yunc.base.utils;

import cn.hutool.core.collection.CollUtil;
import com.yunc.modules.sys.entity.SysDictData;
import com.yunc.modules.sys.service.ISysDictDataService;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author taocongcong
 * @create 2019-10-18 15:40
 */
public class SysDictUtil {

    public static List<SysDictData> listDataByType(String dictType) {
        ISysDictDataService sysDictDataService = (ISysDictDataService) SpringContextUtils.getBean("sysDictDataService");
        // 全部
        List<SysDictData> sysDictDataList = sysDictDataService.listAll();
        if (CollUtil.isEmpty(sysDictDataList)) {
            return new ArrayList<>();
        }
        List<SysDictData> list = new ArrayList<>();
        for (SysDictData sysDictData : sysDictDataList) {
            if (StringUtils.equals(sysDictData.getDictType(), dictType)) {
                list.add(sysDictData);
            }
        }
        return list;
    }

    public static String getNameByValue(String dictType, String value) {
        String name = "";
        // 全部
        List<SysDictData> sysDictDataList = listDataByType(dictType);
        if (CollUtil.isEmpty(sysDictDataList)) {
            return name;
        }
        for (SysDictData sysDictData : sysDictDataList) {
            if (StringUtils.equals(sysDictData.getValue(), value)) {
                name = sysDictData.getName();
                break;
            }
        }
        return name;
    }
}
